// gjw/pages/weipai/paimai_info.js
const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: ['http://www.xgyataigj.net/uploads/180522/4-1P5221A55A24.jpg', 'http://www.xgyataigj.net/uploads/180522/4-1P5221A524234.jpg','http://www.xgyataigj.net/uploads/180522/4-1P5221A414923.jpg'],
    indicatorDots: true,
    autoplay: false,
    interval: 3000,
    duration: 500,
		circular: true,
		sysbzj: true,

    tan: true, 
    zan: '../images/zan.png',
    zanTxt: '我要点赞',
    css: 'goods_zan',

    out_list:[
      { img:'https://img04.sogoucdn.com/app/a/100520093/e18d20c94006dfe0-feec70b0eb485633-bec953997d224b9a1fae638a7fbf2cce.jpg',user:'小明',price:1988,time:'18-08-06 12:32:59'}
    ],
    rule:[
      { ico: '../images/paimai.png', txt: '限时拍卖  价高者得' },
      { ico: '../images/bao.png', txt: '担保交易  为您的出价保价护航' },
      { ico: '../images/zhen.png', txt:'拍卖无忧'},
    ],
    gz: 0,
    tag: '',
    star: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
    options.img = options.img.split(',')
    options.time = options.time.slice(0,11)
    app.location_attachurl(this)
    app.location_url(this)
		var siteUrl = wx.getStorageSync('siteUrl')
    that.setData({
      info:options,
      siteUrl: siteUrl,
      s_agree: options.agree,
      view: options.view
    })
		app.util.request({
			url: 'entry/wxapp/Sysbzj',
			data: { openid: wx.getStorageSync('openid') },
			success: function (res) {
				console.log(res)
				if (res.data.total >= 2 && res.data.pay!=2) {
					that.setData({
						sysbzj: false
					})
				}else{
					that.setData({
						sysbzj: true
					})
				}
			}
		})
    that.getData()
    that.dz()
    that.gz()
    that.view()
    that.chujia()
    that.countdown()
    that.rule()
  },


  // 查看大图
  previewImage: function (e) {
    var that = this
    var url = that.data.siteUrl   // {{siteUrl}}attachment/wff/    地址前缀
    var urls = []
    var current
    var index = e.currentTarget.dataset.index   //当前点击的下标
    var pictures = that.data.info.img

    for (let i in pictures) {
      urls.push(url + 'attachment/' + pictures[i])
    }
    wx.previewImage({
      current: url + 'attachment/' + pictures[index],
      urls: urls
    })
  },


  pai:function(e){
    var that=this
    var show = false
    var hide=true
		app.util.request({
			url: 'entry/wxapp/Isregister',
			data: { openid: wx.getStorageSync('openid') },
			success: function (res) {
				
				if (res.data.code == 200) {
					that.setData({
						tan: show
					})
				} else {
					wx.redirectTo({
						url: '../index/register',
					})
				}
			}
		})
  },

  //规则
  rule:function(e){
    var that=this
    app.util.request({
      url:'entry/wxapp/shotrule',
      success:function(res){
        
        if(res.data.code==200){
          that.setData({
            rule:res.data.res
          })
        }
      }
    })
  },

  // 判断是否关注
  gz: function (e) {
    var that = this
    var gz = that.data.gz
    var openid = wx.getStorageSync('openid')
    var phone = that.data.info.phone
    if (gz == 0) {
      app.util.request({
        url: 'entry/wxapp/Islovestore',
        data: {
          openid: openid, phone: phone
        },
        success: function (res) {
          
          if (res.data.code == 500) {
            that.setData({
              gz: 0
            })
          } else if (res.data.code == 200)
            that.setData({
              gz: 1
            })
        }
      })
    }
  },

  // 判断是否已经点赞
  dz: function (e) {
    var that = this
    var openid = wx.getStorageSync('openid')
    var g_id = that.data.info.id
    
    app.util.request({
      url: 'entry/wxapp/Shotagreedetail',
      data: { openid: openid, id: g_id },
      success: function (res) {
        
        if (res.data.code == 200) {
          that.setData({
            zan: '../images/zan_on.png',
            zanTxt: '已点赞',
            css: 'on_zan',
          })
        }
      }
    })
  },


  // 点赞
  clickZan: function (e) {
    var that = this
    var openid = wx.getStorageSync('openid')
    var g_id = that.data.info.id
    
    app.util.request({
      url: 'entry/wxapp/Shotagree',
      data: { openid: openid, id: g_id },
      success: function (res) {
        
          if(res.data.code==200){
            wx.showToast({
              title: '点赞成功',
            })
            that.setData({
              zan: '../images/zan_on.png',
              zanTxt: '已点赞',
              css: 'on_zan',
              s_agree:res.data.res
            })
        }else if (res.data.code == 201) {
          wx.showToast({
            title: '您已经点过赞了',
            icon: 'none',
          })
        }
      }
    })


  },

  // 浏览量
  view: function (e) {
    var that = this
    var openid = wx.getStorageSync('openid')
    var g_id = that.data.info.id
    app.util.request({
      url: 'entry/wxapp/shotview',
      data: { openid: openid, id: g_id },
      success: function (res) {
        
        if (res.data.code == 200) {
          that.setData({
            view: res.data.res
          })
        }
      }
    })
  },
  

  // 时间
  countdown: function (time) {
    var that = this
    var EndTime = that.data.info.totimesjc
    var NowTime = Math.round(new Date().getTime() / 1000)
    var total_micro_second = EndTime - NowTime
    // console.log(EndTime)
    // console.log(NowTime)
    // console.log(total_micro_second)
    if (total_micro_second <= 0) {   //倒计时结束
      // app.util.request({
      //   url: 'entry/wxapp/UpdateGroup',
      //   data: {
      //     store_id: that.data.id
      //   },
      //   success: res => {
      //     
      //   }
      // })
      // that.setData({
      //   clock: false
      // });

      that.setData({
        jitime:0
      })

    } else if (total_micro_second > 0) {
      that.dateformat(total_micro_second)
      setTimeout(function () {
        total_micro_second -= 1000;
        that.countdown(time);
      }, 1000)
    }

  },

  dateformat: function (e) {
    var that = this
    // 总秒数
    var second = Math.floor(e);
    // 天数
    var day = Math.floor(second / 3600 / 24);
    // 小时
    var hr = Math.floor(second / 3600 % 24);
    // 分钟
    var min = Math.floor(second / 60 % 60);
    // 秒
    var sec = Math.floor(second % 60);
    if (day < 10) {
      day = '0' + day
    }
    if (hr < 10) {
      hr = '0' + hr
    }
    if (sec < 10) {
      sec = '0' + sec
    }
    if (min < 10) {
      min = '0' + min
    }
    that.setData({
      day: day,
      hour: hr,
      min: min,
      sec: sec
    })
  },


  // 查询出价者
  chujia:function(e){
    var that=this
    var w_id=that.data.info.id
    app.util.request({
      url:'entry/wxapp/Shotuser',
      data:{
        shot_id:w_id
      },
      success:function(res){
        
        if(res.data.code==500){    //没有出价者
          that.setData({
            noData:0
          })
        } else if (res.data.code == 200) {    //有出价者
          that.setData({
            noData: 1,
            out_list: res.data.res
          })
        }
      }
    })
  },
 
  // 关注
  follow: function (e) {
    var that = this
    var gz = that.data.gz
    var openid = wx.getStorageSync('openid')
    var phone = that.data.info.phone
    if (gz == 0) {
      app.util.request({
        url: 'entry/wxapp/Lovestore',
        data: {
          openid: openid, phone: phone
        },
        success: function (res) {
          
          that.setData({
            gz: 1
          })
          wx.showToast({
            title: '关注成功',
            icon: 'success'
          })
        }
      })
    } else {
      wx.showToast({
        title: '您已经关注该店铺',
        icon: 'none'
      })
    }
  },

  // 取消出价弹窗
  quxiao:function(e){
    var hide=true
    this.setData({
      tan: hide
    })
  },


  test:function(e){},

  // 出价
  sub:function(e){
    var that=this
    var formid = e.detail.formId
    var openid = wx.getStorageSync('openid')
    var w_id=that.data.info.id
    var linder = that.data.out_list[0].openid     //获取领先者的openid
    if (openid == linder){
      wx.showModal({
        title: '提示',
        content: '你已经出价，且是当前领先者，目前不能继续出价',
        showCancel:false,
        success:function(res){
          if(res.confirm){
           that.quxiao()           
          }
        }
      })
    }else{
      app.util.request({
        url: 'entry/wxapp/Shotfee',
        data: {
          openid: openid, shot_id: w_id, formid: formid
        },
        success:function(res){
          
          var res_id = res.data.res
          
          if (res.data.code == 201 || res.data.code == 202 || res.data.code == 203){
            app.util.request({
              url:'entry/wxapp/Shotbzjpay',
              data:{
                openid: openid, res_id: res_id, shot_id:w_id
              },
              success:function(res){
                
                wx.requestPayment({
                  timeStamp: res.data.timeStamp,
                  nonceStr: res.data.nonceStr,
                  package: res.data.package,
                  signType: res.data.signType,
                  paySign: res.data.paySign,
                  'success': function (res) {
                    
                    app.util.request({
                      url: 'entry/wxapp/Shotbzjpaysuccess',
                      data: {
                        res_id: res_id
                      },
                      success: function (res) {
                        wx.showToast({
                          title: '您已经成功出价啦~',
                          ico:'success',
                          duration:5000
                        })
                        that.quxiao()
                        that.chujia()
                      }
                    })

                  },
                  'fail': function (res) {
                    
                    wx.showToast({
                      title: '支付失败',
                      icon: "loading",
                    })
                  },
                  'complete': function (res) {
                    
                    wx.showToast({
                      title: '取消支付',
                      icon: "loading",
                    })
                  }
                })
              }
            })
            // wx.showToast({
            //   title: '您已经成功出价啦~',
            //   ico:'success',
            // })
            // that.quxiao()
            // that.chujia()
          }
        }
      })
    }
  },
	sysbzj: function (e) {
		var that = this
		var formid = e.detail.formId
		var openid = wx.getStorageSync('openid')
		app.util.request({
			url: 'entry/wxapp/sysbzjpay',
			data: {
				openid: openid,formid:formid
			},
			success: function (res) {
				wx.requestPayment({
					timeStamp: res.data.timeStamp,
					nonceStr: res.data.nonceStr,
					package: res.data.package,
					signType: res.data.signType,
					paySign: res.data.paySign,
					'success': function (pres) {
						app.util.request({
							url: 'entry/wxapp/sysbzjpaysuccess',
							data: {
								res_id: res.data.res_id
							},
							success: function (ores) {
								that.setData({
									sysbzj:true
								})
								wx.showToast({
									title: '您已经支付成功!',
									ico: 'success',
									duration: 5000
								})
							}
						})

					},
					'fail': function (res) {
						wx.showToast({
							title: '支付失败',
							icon: "loading",
						})
					}
				})
			}
		})
	},

  // 店铺评分
  getData: function (e) {
    var that = this
    var phone = that.data.info.phone
    app.util.request({
      url: 'entry/wxapp/Lovestoreinfo',
      data: {
        phone: phone
      },
      success: function (res) {
        
        that.setData({
          tag: res.data
        })
        var num = that.data.tag.star
        var star = []
        for (var i = 0; i < num; i++) {
          star.push('../images/star_on.png')
        }
        that.setData({
          star: star
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
	onReady: function () {
		this.videoContext = wx.createVideoContext('myVideo')
	},
	play: function (e) {
		this.videoContext.requestFullScreen()
	},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})